package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.ActivityItemMapper;
import cn.wolfcode.mapper.ActivityMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.UserContextUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityItemMapper activityItemMapper;


    @Override
    public void save(Activity activity) {
        activityMapper.insert(activity);
    }

    /**
     * 删除投票活动时, 同时删除投票选项和用户投票情况中间表数据
     * @param id 投票活动ID
     */
    @Override
    public void delete(Long id) {
        activityItemMapper.deleteByAid(id);
        activityMapper.deleteRelation(id);
        activityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Activity activity) {
        activityMapper.updateByPrimaryKey(activity);
    }

    @Override
    public Activity get(Long id) {
        return activityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Activity> listAll() {
        return activityMapper.selectAll();
    }

    @Override
    public PageInfo<Activity> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Activity> activitys = activityMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Activity>(activitys);
    }

    @Override
    public void updateStatus(Long id) {
        activityMapper.updateStatus(id);
    }

    /**
     * 增加投票活动
     * @param activity 封装投票活动
     * @param itemArr 封装投票选项
     */
    @Override
    public void saveActivity(Activity activity, String[] itemArr) {
        activityMapper.insert(activity);
        if(itemArr != null){
            for (String item : itemArr) {
                if(!StringUtils.isEmpty(item)){
                    ActivityItem activityItem = new ActivityItem();
                    activityItem.setActivity(activity);
                    activityItem.setContent(item);
                    activityItem.setNum(0);
                    activityItemMapper.insert(activityItem);
                }
            }
        }
    }

    /**
     * 更新投票情况
     * @param activityId 投票内容ID
     * @param itemIds 投票内容选项ID
     */
    @Override
    public void increaseNum(Long activityId, Long[] itemIds) {
        int count = activityMapper.queryShowCount(activityId, UserContextUtil.getCurrentEmployee().getId());
        if(count <= 0){
            activityMapper.insertRelation(activityId, UserContextUtil.getCurrentEmployee().getId());
            if(itemIds != null){
                for (Long aLong : itemIds) {
                    activityItemMapper.increaseNum(aLong);
                }
            }
        }else{
            throw new BusinessException("该活动您已投票,请勿重复操作");
        }
    }

    /**
     * 用户点击投票限制逻辑
     * @param id 投票活动ID
     */
    @Override
    public void checkin(Long id) {
        Activity activity = activityMapper.selectByPrimaryKey(id);
        if(!activity.isStatus()){
            throw new BusinessException("该活动已停止投票");
        }
        int count = activityMapper.queryShowCount(id, UserContextUtil.getCurrentEmployee().getId());
        if(count > 0){
            throw new BusinessException("该活动您已投票,请勿重复操作");
        }
    }

    /**
     * 用户点击查看限制逻辑,超级管理员跳过限制
     * @param id 投票活动ID
     */
    @Override
    public void seeActivity(Long id) {
        if(!UserContextUtil.getCurrentEmployee().isAdmin()){
            Activity activity = activityMapper.selectByPrimaryKey(id);
            if(activity.isStatus()){
                int count = activityMapper.queryShowCount(id, UserContextUtil.getCurrentEmployee().getId());
                if(count <= 0){
                    throw new BusinessException("当前活动您还未投票,请先投票");
                }
            }
        }
    }
}
